<?php
/**
 * 小说Dao
 */

class Dao_UserModel extends Dao_BaseModel {

    protected $_table = 'user';

    const NORMAL_STATUS = 1;
    const INFO_KEYWORD = 'USER:INFO_KEY_';
    const EXTIME        = '86400';

    public function __construct ()
    {
        parent::__construct ();
    }
    
    /**
     * 获取用户信息
     */
    public function getUserInfo($wheres = [])
    {
        $where   = array ();
        if(isset( $wheres['user_phone'] )){
            $where[] = array ( 'user_phone' , $wheres['user_phone'] );
        }
        $info = [];
        $redis = RedisTool::getInstance();
        if(isset( $wheres['user_id'] )){
            $where[] = array ( 'user_id' , $wheres['user_id'] );
//            $info = $redis->get( self::INFO_KEYWORD.$wheres['user_id'] );
        }else{
            $info = [];
        }
        
        if(isset( $wheres['user_password'] )){
            $where[] = array ( 'user_password' , md5($wheres['user_password']) );
        }
        
        $where[] = array ( 'user_status' , self::NORMAL_STATUS );
        
        if( !$info ){
            $info = parent::getOne( array ( 'user_id' , 'user_nickname' , 'user_phone' ,'user_balance','user_balance_used', 'user_sex' , 'user_age' , 'user_province' , 'user_city' , 'user_area' , 'user_img_url' , 'user_balance' ) , $where );
            if( isset( $wheres['user_id'] ) ){
                $redis->set( self::INFO_KEYWORD.$wheres['user_id'] , json_encode($info) );
            }
        }else{
            $info = json_decode($info,true);
        }
        return $info;
    }
    
    /**
     * 获取用户密码
     */
    public function getUserPwd($wheres = [])
    {
        $where[] = array ( 'user_status' , self::NORMAL_STATUS );
        $where[] = array ( 'user_id' , $wheres['user_id'] );
        $info = parent::getOne( array ( 'user_id' , 'user_nickname' , 'user_password' ,'user_phone' , 'user_sex' , 'user_age' , 'user_province' , 'user_city' , 'user_area' , 'user_img_url' , 'user_balance' ) , $where );
        return $info;
    }
    
    /**
     * 新增用户
     */
    public function addUser($data)
    {
        $info = parent::add ( $data );
        return $info;
    }
    
    /**
     * 修改用户
     * @param type $data
     * @return type
     */
    public function editUser( $data , $arr = [] )
    {
        $info = parent::edit( $data , $arr ); 
        return $info;
    }
    
    /**
     * 获取用户是否注册
     * @param str $phone
     * @return type
     */
    public function verifyPhone( $phone )
    {
        $where = array();
        $where[] = array ( 'user_phone' , $phone['user_phone'] );
        $info = parent::getOne( array('user_id','user_password') , $where ); 
        return $info;
    }

}
